﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace NEditorControl
{
    [Description("NEditorControl.LyhHtmlEditor")]
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:LyhHtmlEditor runat=server></{0}:LyhHtmlEditor>")]
    public class LyhHtmlEditor : Panel
    {
        #region 脚本

        private readonly string editor_config = "{0}editor_config.js";
        private readonly string editor_all = "{0}editor_all.js";
        private readonly string editor_css = @" <link href='{0}themes/default/ueditor.css' rel='stylesheet' type='text/css' /> ";
        private static string temp = string.Empty;

        private readonly string editor_init = @"
        <script type=""text/javascript""> 
        //<![CDATA[
                var option = {{
                    initialContent: '{1}',  //初始化编辑器的内容
                    minFrameHeight: {3},    //最小高度
                    elementPathEnabled: {4},  //编辑框下部的path部分
                    maximumWords: {5},  //允许的最大字符数
                    autoFloatEnabled: {6},  //工具栏浮动
                    autoHeightEnabled: {7}, //编辑框部分可以随着编辑内容的增加而自动长高
                    textarea: 'editorcontent'   //设置提交时编辑器内容的名字
                }};
                var {2} = new baidu.editor.ui.Editor(option);
                {2}.render('{0}');
        //]]>
        </script>
        ";

        #endregion

        [Bindable(true)]
        [Description("编辑器中的HTML文本")]
        [Category("自定义属性")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                string s = string.Empty;
                if (DesignMode)
                    s = (string)ViewState["Text"];
                else
                    s = Page.Request["editorcontent"];
                return ((s == null) ? string.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// 脚本文件路径
        /// </summary>
        [
        Description("资源目录相对路径"),
        Category("自定义属性"),
        DefaultValue("/NEditor/"),
        ]
        public string ScriptPath
        {
            get
            {
                object o = ViewState["ScriptPath"];
                return (o != null ? (string)o : "/NEditor/");
            }
            set
            {
                ViewState["ScriptPath"] = value;
            }
        }

        /// <summary>
        /// 工具栏配置
        /// </summary>
        [
        Description("工具栏配置名称"),
        Category("自定义属性"),
        DefaultValue("CustomFull"),
        ]
        public string Toolbar
        {
            get
            {
                object o = ViewState["Toolbar"];
                return (o != null ? (string)o : "CustomFull");
            }
            set
            {
                ViewState["Toolbar"] = value;
            }
        }

        /// <summary>
        /// 编辑框配置
        /// </summary>
        [
        Description("编辑框下部的path部分"),
        Category("自定义属性"),
        DefaultValue(true),
        ]
        public bool ElementPathEnabled
        {
            get
            {
                object o = ViewState["ElementPathEnabled"];
                return (o != null ? (bool)o : true);
            }
            set
            {
                ViewState["ElementPathEnabled"] = value;
            }
        }

        /// <summary>
        /// 编辑框配置
        /// </summary>
        [
        Description("让编辑器的编辑框部分可以随着编辑内容的增加而自动长高"),
        Category("自定义属性"),
        DefaultValue(true),
        ]
        public bool AutoHeightEnabled
        {
            get
            {
                object o = ViewState["AutoHeightEnabled"];
                return (o != null ? (bool)o : true);
            }
            set
            {
                ViewState["AutoHeightEnabled"] = value;
            }
        }

        /// <summary>
        /// 编辑框配置
        /// </summary>
        [
        Description("工具栏浮动"),
        Category("自定义属性"),
        DefaultValue(true),
        ]
        public bool AutoFloatEnabled
        {
            get
            {
                object o = ViewState["AutoFloatEnabled"];
                return (o != null ? (bool)o : true);
            }
            set
            {
                ViewState["AutoFloatEnabled"] = value;
            }
        }

        /// <summary>
        /// 编辑框配置
        /// </summary>
        [
        Description("允许的最大字符数"),
        Category("自定义属性"),
        DefaultValue(10000),
        ]
        public int MaximumWords
        {
            get
            {
                object o = ViewState["MaximumWords"];
                return (o != null ? (int)o : 10000);
            }
            set
            {
                ViewState["MaximumWords"] = value;
            }
        }

        /// <summary>
        /// 皮肤名称
        /// </summary>
        [
        Description("皮肤名称"),
        Category("自定义属性"),
        DefaultValue("office2003"),
        ]
        public string SkinName
        {
            get
            {
                object o = ViewState["SkinName"];
                return (o != null ? (string)o : "office2003");
            }
            set { ViewState["SkinName"] = value; }
        }

        /// <summary>
        /// 控件高度
        /// </summary>
        [
        Description("控件高度"),
        Category("自定义属性"),
        DefaultValue("200"),
        ]
        public override Unit Height
        {
            get
            {
                object o = ViewState["Height"];
                return (o != null ? (Unit)o : new Unit(200));
            }
            set
            {
                ViewState["Height"] = value;
            }
        }

        /// <summary>
        /// 控件宽度
        /// </summary>
        [
        Description("控件宽度"),
        Category("自定义属性"),
        DefaultValue("700"),
        ]
        public override Unit Width
        {
            get
            {
                object o = ViewState["Width"];
                return (o != null ? (Unit)o : new Unit(700));
            }
            set
            {
                ViewState["Width"] = value;
            }
        }

        /// <summary>
        /// CSS样式
        /// </summary>
        [
        Description("样式"),
        Category("自定义属性"),
        DefaultValue("ckeditor"),
        ]
        public override string CssClass
        {
            get
            {
                return base.CssClass;
            }
            set
            {
                base.CssClass = value;
            }
        }

        /// <summary>
        /// 摘要:如果 System.Web.UI.WebControls.TextBox.AutoPostBack 是 true，则在客户端上呈现之前注册用于生成回发事件的客户端脚本。
        /// 参数:e:
        /// 包含事件数据的 System.EventArgs。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            this.Attributes.CssStyle.Add(HtmlTextWriterStyle.Width, Width.ToString());
            this.Attributes.CssStyle.Add(HtmlTextWriterStyle.Height, Height.ToString());

            /*注册javacript脚本的几种方法*/
            /*
            string name = "myJavaScriptCode.js";
            Page.ClientScript.RegisterClientScriptInclude("myKey", name);*/
            /*
            // 引入js文件
            HtmlGenericControl scriptControl = new HtmlGenericControl("script");
            scriptControl.Attributes.Add("type", "text/javascript");
            scriptControl.Attributes.Add("language", "JavaScript");
            scriptControl.Attributes.Add("src", "../js/menu.js");
            Page.Header.Controls.Add(scriptControl);
             */

            ClientScriptManager csm = Page.ClientScript;
            string fullPath = ResolveUrl(@"~" + this.ScriptPath);

            string f_editor_css = string.Format(editor_css, fullPath);
            if (!this.Page.ClientScript.IsStartupScriptRegistered("editor_css"))
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "editor_css", f_editor_css, false);
            }

            //注册配置脚本
            string f_editor_config = string.Format(editor_config, fullPath);
            if (!csm.IsClientScriptIncludeRegistered("editor_config"))
                csm.RegisterClientScriptInclude(this.GetType(), "editor_config", f_editor_config);

            //注册功能脚本
            string f_editor_all = string.Format(editor_all, fullPath);
            if (!csm.IsClientScriptIncludeRegistered("editor_all"))
                csm.RegisterClientScriptInclude(this.GetType(), "editor_all", f_editor_all);

            //初始化组件脚本
            string f_editor_init = string.Format(editor_init, this.ClientID, (string)ViewState["Text"], this.ID, Height.Value, ElementPathEnabled.ToString().ToLower(), MaximumWords, AutoFloatEnabled.ToString().ToLower(), AutoHeightEnabled.ToString().ToLower());
            if (!csm.IsStartupScriptRegistered("editor_init"))
                csm.RegisterStartupScript(this.GetType(), "editor_init", f_editor_init, false);
        }
    }
}
